package com.tianqu.tms.www.attn.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Attn;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.attn.viewhelper.AttnViewhelper;

/**
 * @author zhangxueliang
 * 经办人详细信息controller
 */
public class AttnDetailInfoController extends TmsController{

	private Logger logger = Logger.getLogger(AttnDetailInfoController.class);
	private static final String ATTNID = "attnId";
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			String attnId = "";
			attnId = (String)uc.getAttribute(ATTNID);
			Attn attn = Attn.getInstance();
			Member mem = Member.getInstance();
			attn = attn.findAttnByAttnId(attnId);
			User user = User.getInstance();
			user = user.getUserProfileByLoginid(attn.getUserId(), TmsConstants.BLANK);
			
			AttnViewhelper viewhelper = new AttnViewhelper();
			if(user!=null){
				if(user.getFlag() == 1){
					viewhelper.setMemStatus("正常");
				}else if(user.getFlag() == 3){
					viewhelper.setMemStatus("禁用");
				}
			}
			viewhelper.setAttnId(attn.getAttnId());
			viewhelper.setAttnName(attn.getAttnName());
			viewhelper.setAttnIdCardNo(attn.getAttnIdCardNo());
			viewhelper.setAttnPhone(attn.getAttnPhone());
			viewhelper.setAttnPhoto(attn.getAttnPhoto());
			viewhelper.setAccessCardId(attn.getAccessCardId());
			viewhelper.setAccessCardNo(attn.getAccessCardNo());
			viewhelper.setUserId(attn.getUserId());
			viewhelper.setAttnAddress(attn.getAttnAddress());
			viewhelper.setMemberId(mem.getMemNameByMemId(Integer.parseInt(attn.getMemberId())));
			uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}

}
